<?php
/**
 * 谈情说爱模块
 */

class Module_amorism{
	static private $white_uins = array(1 , 2 , 3 , 9);

	/**
	 * 构造函数
	 */
	function __construct() {
	
	}
	
	function index() {
		return $this->show();
	}


	/**
	*谈情说爱显示
	*/
	function show() {
		$page = isset($_GET['page']) ? intval($_GET['page']) : null;
		$uin = SessionInfo('uin');
		
		$smarty = loadSmarty();
		
		// 获取用户数据
		loadLib('Amorism');
		//获取数据总量
		$amorism_count = Amorism::getCount();
		
		//设置每行10条数据
		$per_page = 10;
		if($page <= 0){
			$page = 1;
		}
		
		//计算总页数
		$amorism_list = Amorism::getList($page , $per_page);
		
		$send_uin_list = array();
		foreach($amorism_list as $row) {
			$send_uin_list[] = $row['uin'];

		}

		$is_white_uin = $this->_is_white_uin($uin);
		//print_r($send_uin_list);
		if(!empty($send_uin_list)) {
			loadLib('User');
			
			$send_uin_info = User::getBaseInfo($send_uin_list);
			
			foreach($amorism_list as $k => $row) {
				$send_uin = $row['uin'];
				
				$amorism_list[$k]['send_uin_name'] = isset($send_uin_info[$send_uin]) ? $send_uin_info[$send_uin]['name'] : '';
				$amorism_list[$k]['send_uin_sex'] = isset($send_uin_info[$send_uin]) ? $send_uin_info[$send_uin]['sex'] : '';
				$amorism_list[$k]['can_reply'] = $is_white_uin == $uin ? true : false;
				
				//计算已回复人个数
				$amorism_list[$k]['count'] = count($amorism_list[$k]['reply']);
			}
		}
		
		$smarty->assign('amorism_count' , $amorism_count);
		$smarty->assign('amorism_list' , $amorism_list);
		$smarty->assign('per_page' , $per_page);

		return $smarty->fetch('amorism/index.tpl.php');
	}


	/**
	*谈情说爱写入
	*/
	function add() {
		$content = isset($_POST['content']) ? trim($_POST['content']) : null;
		
		// 检查是否有内容
		if($content === null || strlen($content) == 0) {
			return View::error('内容不能为空');
		}
		
		// 长度校验
		if(strlen($content) > 255) {
			return View::error('内容超过了长度限制，最长是140个字');
		}
		
		$uin = SessionInfo('uin');

		loadLib('Amorism');
		$code = Amorism::add($uin , $content);
		
		if($code === 0) {
			loadLib('Operate');
			Operate::getAdd($uin , 0 , 'amorism');		
			return View::success('发表成功');
		} else {
			return View::error('系统繁忙，请稍后再试' , $code);
		}
	}
	/**
	 * 跳转回复页面
	 */
	function setsend(){
		$people_uin = isset($_POST['uin']) ? intval($_POST['uin']) : null;
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
		header('Location: ' . SITEURL . '/?m=amorism&a=send&uin=' . $people_uin . '&id=' . $id . '&sid=' . sessionInfo('skey') );
	}
	/**
	*谈情说爱回复界面
	*/
	function send(){
		$page = isset($_GET['page']) ? intval($_GET['page']) : null;
		$people_uin = isset($_GET['uin']) ? intval($_GET['uin']) : null;
		$id = isset($_GET['id']) ? intval($_GET['id']) : null;
		//print_r($people_uin);
		$uin = SessionInfo('uin');
		
		$smarty = loadSmarty();
		
		// 获取投诉数据
		loadLib('Amorism');
		//获取数据
		$amorism_send = Amorism::getSend($id , $people_uin);
		
		if(empty($amorism_send['id'])){
			return View::error('该信息不存在或已被删除' );
		}
		
			$send_uin_list = array();
			$send_uin_list[] = $amorism_send['uin'];
				
				if(!empty($amorism_send['reply'])) {
					foreach($amorism_send['reply'] as $reply_row) {
						$send_uin_list[] = $reply_row['uin'];
					}
				}
			
			if(!empty($send_uin_list)) {
				loadLib('User');
				$send_uin_info = User::getBaseInfo($send_uin_list);
				
				$send_uin = $amorism_send['uin'];
				
				$amorism_send['send_uin_name'] = isset($send_uin_info[$send_uin]) ? $send_uin_info[$send_uin]['name'] : '';
				$amorism_send['send_uin_sex'] = isset($send_uin_info[$send_uin]) ? $send_uin_info[$send_uin]['sex'] : '';
				
				if(!empty($amorism_send['reply'])) {
					foreach($amorism_send['reply'] as $j => $reply_row) {
						$reply_uin = $reply_row['uin'];	
						$amorism_send['reply'][$j]['name'] = isset($send_uin_info[$reply_uin]) ? $send_uin_info[$reply_uin]['name'] : '';
						$amorism_send['reply'][$j]['sex'] = isset($send_uin_info[$reply_uin]) ? $send_uin_info[$reply_uin]['sex'] : '';
						$amorism_send['reply'][$j]['id'] = $j + 1;
					}
				}
			}
			
		//设置每行10条数据
			$per_page = 10;
			if($page <= 0){
				$page = 1;
			}
			$count = count($amorism_send['reply']);
		//	print_r($amorism_send['reply']);
			$page = ($page-1) * $per_page;
			//print_r($page);
			$send = array_slice($amorism_send['reply'],$page,$per_page);
		//print_r($send);
			$smarty->assign('amorism_send' , $amorism_send);
			$smarty->assign('send' , $send);
			$smarty->assign('count' , $count);
			$smarty->assign('id' , $id);
			$smarty->assign('people_uin' , $people_uin);
			$smarty->assign('per_page' , $per_page);
			return $smarty->fetch('amorism/send.tpl.php');
		
	}
	/**
	*谈情说爱回复操作
	*/
	function upSend(){
		$people_uin = isset($_POST['uin']) ? intval($_POST['uin']) : null;
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
		$content = isset($_POST['content']) ? trim($_POST['content']) : null;
		
		if(empty($id)) {
			return View::error('参数错误' );
		}
		
		// 检查是否有内容
		if($content === null || strlen($content) == 0) {
			return View::error('内容不能为空');
		}
		
		// 长度校验
		if(strlen($content) > 255) {
			return View::error('内容超过了长度限制，最长是140个字');
		}

		$uin = SessionInfo('uin');
		
		loadLib('Amorism');
		//print_r($people_uin);
		$org_amorism = Amorism::getSend($id , $people_uin);
		//print_r($org_amorism);
		if(empty($org_amorism)) {
			return View::error('该信息不存在或已被删除');
		}

		//print_r($org_amorism);
		$code = Amorism::getUpSend($uin , $people_uin , $id , $content , $org_amorism);
		
		if($code === 0) {
			if($uin != $people_uin) {
				loadLib('Operate');
				Operate::getAdd($uin , $people_uin , 'amorism_reply');
				$sysmsg_to_uin = $org_amorism['uin'];
				loadLib('SysMsg');
				SysMsg::sendSystem($sysmsg_to_uin , array(
					'send_uin' => $uin,
					'id' => $id,
				) , 1009);
			}		
			header('Location: ' . SITEURL . '/?m=amorism&a=send&uin=' . $people_uin . '&id=' . $id . '&sid=' . sessionInfo('skey') );
		} else {
			return View::error('该信息已删除，操作失败' , $code);
		}
	}

	/**
	 * 是否是白名单用户
	 */
	private function _is_white_uin($uin) {
		return in_array($uin , self::$white_uins);
	}
	/**
	*删除谈情说爱
	*/
	function delete(){
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
		$people_uin = isset($_POST['uin']) ? intval($_POST['uin']) : null;
		//print_r($id);
		if(empty($id)) {
			return View::error('参数错误' );
		}
		$uin = SessionInfo('uin');
	//	print_r($id);
		if( !$this->_is_white_uin($uin) ) {
			return View::error('您不是管理员，没有删除权限');
		}
		loadLib('Amorism');
		$code = Amorism::delete($id , $people_uin);
			
		if($code === 0) {		
			return View::success('已成功删除该信息');
		} else {
			return View::error('系统繁忙' , $code);
		}
		
	}
}


# end of script
